package com.example.ffepeg.mp3video;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author TS
 * @date 2024/11/1 10:38
 */
public class Mp3Video {
    public static void clipAudio(String inputFile, String outputFile, String startTime, String duration) {
        try {
            // 构建FFmpeg命令行
            String command = "ffmpeg -i " + inputFile + " -ss " + startTime + " -t " + duration + " -acodec copy " + outputFile;
            // 执行命令
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(command);
            // 读取错误流
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;
            while ((line = errorReader.readLine()) != null) {
                System.out.println("FFmpeg Error: " + line);
            }
            // 等待进程结束
            process.waitFor();
            System.out.println("Audio clipped successfully.");

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String imp3 = "https://bpic.588ku.com/audio_library/watermark/24/07/18/2faa3006ff1694099a0c3315b6c683f2.mp3";
        // 音频文件路径，输出文件路径，开始时间（例如："00:01:30"），时长（例如："00:00:10"）
        clipAudio(imp3, "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\output.mp3", "00:00:03", "00:00:12");
    }
}
